<template>
  <div class="radar-chart">
    <div class="chart-title">{{ title }}</div>
    <div class="radar-container">
      <div ref="radarChart" class="radar-chart-content"></div>
      <div class="radar-legend">
        <div class="legend-item" v-for="item in legendData" :key="item.name">
          <div class="legend-color" :style="{ backgroundColor: item.color }"></div>
          <span class="legend-text">{{ item.name }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'RadarChart',
  props: {
    title: {
      type: String,
      default: '雷达图'
    },
    data: {
      type: Array,
      default: () => [
        {
          name: '产品A',
          value: [85, 72, 68, 90, 75, 82],
          color: '#00d4ff'
        },
        {
          name: '产品B',
          value: [78, 88, 75, 82, 70, 85],
          color: '#00ff88'
        },
        {
          name: '产品C',
          value: [92, 65, 80, 75, 88, 70],
          color: '#ff6b6b'
        }
      ]
    },
    indicators: {
      type: Array,
      default: () => [
        { name: '性能', max: 100 },
        { name: '易用性', max: 100 },
        { name: '稳定性', max: 100 },
        { name: '安全性', max: 100 },
        { name: '扩展性', max: 100 },
        { name: '性价比', max: 100 }
      ]
    }
  },
  data() {
    return {
      chart: null,
      legendData: []
    }
  },
  mounted() {
    this.initLegend()
    this.initChart()
    this.startAnimation()
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose()
    }
    if (this.animationTimer) {
      clearInterval(this.animationTimer)
    }
  },
  methods: {
    initLegend() {
      this.legendData = this.data.map(item => ({
        name: item.name,
        color: item.color
      }))
    },
    initChart() {
      this.chart = echarts.init(this.$refs.radarChart)
      
      const option = {
        backgroundColor: 'transparent',
        radar: {
          indicator: this.indicators,
          radius: '70%',
          splitNumber: 5,
          name: {
            textStyle: {
              color: '#fff',
              fontSize: 12
            }
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.2)'
            }
          },
          splitArea: {
            show: true,
            areaStyle: {
              color: ['rgba(0, 212, 255, 0.1)', 'rgba(0, 212, 255, 0.05)']
            }
          },
          axisLine: {
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.3)'
            }
          }
        },
        series: [
          {
            type: 'radar',
            data: this.data.map(item => ({
              value: item.value,
              name: item.name,
              itemStyle: {
                color: item.color
              },
              areaStyle: {
                color: item.color + '30'
              },
              lineStyle: {
                color: item.color,
                width: 2
              }
            }))
          }
        ]
      }
      
      this.chart.setOption(option)
    },
    startAnimation() {
      // 模拟数据更新动画
      this.animationTimer = setInterval(() => {
        const updatedData = this.data.map(item => ({
          ...item,
          value: item.value.map(val => Math.max(0, Math.min(100, val + (Math.random() - 0.5) * 10)))
        }))
        
        this.chart.setOption({
          series: [{
            data: updatedData.map(item => ({
              value: item.value,
              name: item.name,
              itemStyle: {
                color: item.color
              },
              areaStyle: {
                color: item.color + '30'
              },
              lineStyle: {
                color: item.color,
                width: 2
              }
            }))
          }]
        })
      }, 5000)
    },
    resize() {
      if (this.chart) {
        this.chart.resize()
      }
    }
  }
}
</script>

<style scoped>
.radar-chart {
  width: 100%;
  height: 100%;
  padding: 20px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}

.chart-title {
  color: #fff;
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 20px;
  text-align: center;
}

.radar-container {
  flex: 1;
  display: flex;
  gap: 20px;
  position: relative;
}

.radar-chart-content {
  flex: 1;
  height: 100%;
  min-height: 250px;
}

.radar-legend {
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(0, 0, 0, 0.6);
  padding: 15px;
  border-radius: 8px;
  border: 1px solid rgba(0, 212, 255, 0.3);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.legend-item:last-child {
  margin-bottom: 0;
}

.legend-color {
  width: 12px;
  height: 12px;
  border-radius: 2px;
}

.legend-text {
  color: #fff;
  font-size: 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .radar-container {
    flex-direction: column;
  }
  
  .radar-legend {
    position: static;
    margin-top: 10px;
  }
}
</style>
